package com.adtec.base.cloud.baseservice.rbac.system.controller;

import com.adtec.base.cloud.baseservice.rbac.system.service.SysUserService;
import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.persistence.system.model.SysUser;
import com.adtec.base.cloud.persistence.system.model.SysUserOrg;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/sysuser")
@Tag(name = "sysuser", description = "用户管理")
public class SysUserController {

    @Resource
    private SysUserService sysuserService;

    //根据登录用户名查询用户信息
    @GetMapping(value = "/info")
    @Operation(description = "根据登录用户名查询用户信息")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
    })
    public SysUser info(@RequestParam("username") String username) {
        return sysuserService.getUserByUserName(username);
    }

    //用户管理：查询
    @PostMapping("/query")
    @Operation(description = "用户管理：查询")
    @Parameters({
            @Parameter(name = "orgId", description = "组织ID"),
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "phone", description = "手机号"),
            @Parameter(name = "email", description = "邮箱"),
            @Parameter(name = "enabled", description = "是否禁用"),
            @Parameter(name = "createStartTime", description = "创建时间(区间查询开始)"),
            @Parameter(name = "createEndTime", description = "创建时间(区间查询结束)"),
            @Parameter(name = "pageNum", description = "分页展示页数"),
            @Parameter(name = "pageSize", description = "分页每页页数"),
    })
    public IPage<SysUserOrg> query(@RequestParam(name = "orgId", required = false) Long orgId,
                                   @RequestParam(name = "username", required = false) String username,
                                   @RequestParam(name = "phone", required = false) String phone,
                                   @RequestParam(name = "email", required = false) String email,
                                   @RequestParam(name = "enabled", required = false) Boolean enabled,
                                   @RequestParam(name = "createStartTime", required = false) Date createStartTime,
                                   @RequestParam(name = "createEndTime", required = false) Date createEndTime,
                                   @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {

        return sysuserService.queryUser(orgId, username, phone, email, enabled,
                createStartTime, createEndTime,
                pageNum, pageSize);
    }

    //用户管理：更新
    @PostMapping(value = "/update")
    @Operation(description = "用户管理：更新")
    public AjaxResponse update(@RequestBody SysUser sysUser) {
        sysuserService.updateUser(sysUser);
        return AjaxResponse.success("更新用户成功！");
    }

    //用户管理：新增
    @PostMapping(value = "/add")
    @Operation(description = "用户管理：新增")
    public AjaxResponse add(@RequestBody SysUser sysUser) {
        sysuserService.addUser(sysUser);
        return AjaxResponse.success("新增用户成功！");
    }

    //用户管理：删除
    @PostMapping(value = "/delete")
    @Operation(description = "用户管理：删除")
    public AjaxResponse delete(@RequestParam String username) {
        sysuserService.deleteUser(username);
        return AjaxResponse.success("删除用户成功!");
    }

    //用户管理：重置密码
    @PostMapping(value = "/pwd/reset")
    @Operation(description = "用户管理：重置密码")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
    })
    public AjaxResponse pwdreset(@RequestParam String username) {
        sysuserService.pwdreset(username);
        return AjaxResponse.success("重置密码成功!");
    }

    //判断登录用户密码是否是默认密码
    @PostMapping(value = "/pwd/isdefault")
    @Operation(description = "判断登录用户密码是否是默认密码")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
    })
    public Boolean isdefault(@RequestParam String username) {

        return sysuserService.isdefault(username);
    }

    //修改密码
    @PostMapping(value = "/pwd/change")
    @Operation(description = "修改密码")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "oldPass", description = "旧密码"),
            @Parameter(name = "newPass", description = "新密码"),
    })
    public AjaxResponse pwdchange(@RequestParam String username,
                                  @RequestParam String oldPass,
                                  @RequestParam String newPass) {
        sysuserService.changePwd(username, oldPass, newPass);
        return AjaxResponse.success("修改密码成功!");
    }

    //用户管理：更新用户激活状态
    @PostMapping(value = "/enabled/change")
    @Operation(description = "用户管理：更新用户激活状态")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
    })
    public AjaxResponse update(@RequestParam String username,
                               @RequestParam Boolean enabled) {
        sysuserService.updateEnabled(username, enabled);
        return AjaxResponse.success("用户状态更新成功！");
    }
}